/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RelatoriosCarregar;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Marcos
 */
public class AgendamentoCarregar {
    
    public static void main(String[] args) throws SQLException, JRException {
        new AgendamentoCarregar().abrirRelatorioAgendamento();
    }
    
    private Connection getConnection() throws SQLException {
        Connection con = null;
        try {
            
            Class.forName("org.firebirdsql.jdbc.FBDriver");//Inicializa o Driver
            return DriverManager.getConnection("jdbc:firebirdsql:localhost/3050:C:\\Users\\Firebird\\Google Drive\\Banco\\firebird.fdb", "SYSDBA", "masterkey");//Estsbelece a conexão
            
            /*
            String driver = "org.firebirdsql.jdbc.FBDriver";
            String url = "dbc:firebirdsql:localhost/3050:C:\\Users\\Firebird\\Google Drive\\Banco\\firebird.fdb";
            String user = "SYSDBA";
            String password = "masterkey";

            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            */
        } catch (ClassNotFoundException e) {
            throw new SQLException(e.getMessage());// se houve algum erro, uma exceção é gerada para informar o erro

        }
        //return con;
    }
    
    private void mostrarErro(String msg) {
        JOptionPane.showMessageDialog(null, msg, "Erro encontrado", JOptionPane.ERROR_MESSAGE);
    }

    public void abrirRelatorioAgendamento() throws SQLException, JRException {
        Connection con = getConnection();
        
        Map parametros = new HashMap();
        
        String path = "./Relatorios";
        File rel = new File(path);
        if (!rel.exists()) {
            path = "../Relatorios";
        }        
        //path += "/RelatorioAgendamento.jasper";
        path += "/Agendamento.jasper";
       

        File file = new File(path);
        file = file.getAbsoluteFile();
        String repStr2 = file.getPath();
        try {
            JasperFillManager.fillReportToFile(repStr2, parametros, con);
            JasperPrint jasperPrint = JasperFillManager.fillReport(repStr2, parametros, con);
            JasperViewer viewer = new JasperViewer(jasperPrint, false);
            viewer.setVisible(true);
        } catch (JRException exc) {
            JOptionPane.showMessageDialog(null, exc.getMessage());
            exc.printStackTrace();
        }
        
    }
    
}
